## Lopez-Cordova and Meissner 2008 WP

library(foreign)
library(Amelia)

## Pre-WWI period
## Load original dataset
lcm2008_prewar <- read.dta("LCM2008 WP Rep Data Prewar.dta")
head(lcm2008_prewar)
dim(lcm2008_prewar)

## Drop ID vars
lcm2008_prewar$cty1 <- lcm2008_prewar$marker <- NULL

## Drop highly collinear variables (not in analysis model)
lcm2008_prewar$openness2 <-lcm2008_prewar$polityave <-lcm2008_prewar$open2ave <-lcm2008_prewar$open2sq <-lcm2008_prewar$openhyby_sq <-lcm2008_prewar$t1870 <-lcm2008_prewar$t1875 <-lcm2008_prewar$t1880 <-lcm2008_prewar$t1885 <-lcm2008_prewar$t1890 <-lcm2008_prewar$t1895 <-lcm2008_prewar$t1900 <-lcm2008_prewar$t1905 <-lcm2008_prewar$t1910 <- lcm2008_prewar$open2_resa <-lcm2008_prewar$priepolity <-lcm2008_prewar$open2_resb <-lcm2008_prewar$open2_resc <-lcm2008_prewar$open2_resc_a <-lcm2008_prewar$pansamp <-lcm2008_prewar$lavgdist <-lcm2008_prewar$ncomlang1910 <-lcm2008_prewar$open2landlabor <-lcm2008_prewar$openhybylandlabor <-lcm2008_prewar$c2trancosts <-lcm2008_prewar$c3trancosts <-lcm2008_prewar$c4trancosts <-lcm2008_prewar$c5trancosts <-lcm2008_prewar$c6trancosts <-lcm2008_prewar$c7trancosts <-lcm2008_prewar$c8trancosts <-lcm2008_prewar$c9trancosts <-lcm2008_prewar$c10trancosts <-lcm2008_prewar$c11trancosts <-lcm2008_prewar$c12trancosts <-lcm2008_prewar$c13trancosts <-lcm2008_prewar$c14trancosts <-lcm2008_prewar$c15trancosts <-lcm2008_prewar$c16trancosts <-lcm2008_prewar$c17trancosts <-lcm2008_prewar$c18trancosts <-lcm2008_prewar$c19trancosts <-lcm2008_prewar$c20trancosts <-lcm2008_prewar$c21trancosts <-lcm2008_prewar$c22trancosts <-lcm2008_prewar$c23trancosts <-lcm2008_prewar$c24trancosts <-lcm2008_prewar$c25trancosts <-lcm2008_prewar$c26trancosts <-lcm2008_prewar$c27trancosts <-lcm2008_prewar$c28trancosts <-lcm2008_prewar$c29trancosts <- lcm2008_prewar$c30trancosts <-lcm2008_prewar$c31trancosts <-lcm2008_prewar$c32trancosts <-lcm2008_prewar$c33trancosts <-lcm2008_prewar$c34trancosts <-lcm2008_prewar$c35trancosts <-lcm2008_prewar$c36trancosts <-lcm2008_prewar$c37trancosts <-lcm2008_prewar$c38trancosts <-lcm2008_prewar$c39trancosts <-lcm2008_prewar$c40trancosts <-lcm2008_prewar$c41trancosts <-lcm2008_prewar$c42trancosts <-lcm2008_prewar$c43trancosts <-lcm2008_prewar$c44trancosts <-lcm2008_prewar$c45trancosts <-lcm2008_prewar$c46trancosts <-lcm2008_prewar$c47trancosts <-lcm2008_prewar$c48trancosts <-lcm2008_prewar$c49trancosts <-lcm2008_prewar$c50trancosts <-lcm2008_prewar$c1trancosts <- lcm2008_prewar$c2time <-lcm2008_prewar$c3time <-lcm2008_prewar$c4time <-lcm2008_prewar$c5time <-lcm2008_prewar$c6time <-lcm2008_prewar$c7time <- lcm2008_prewar$c8time <-lcm2008_prewar$c9time <-lcm2008_prewar$c10time <-lcm2008_prewar$c11time <-lcm2008_prewar$c12time <-lcm2008_prewar$c13time <-lcm2008_prewar$c14time <-lcm2008_prewar$c15time <-lcm2008_prewar$c16time <-lcm2008_prewar$c17time <-lcm2008_prewar$c18time <-lcm2008_prewar$c19time <-lcm2008_prewar$c20time <-lcm2008_prewar$c21time <-lcm2008_prewar$c22time <-lcm2008_prewar$c23time <-lcm2008_prewar$c24time <-lcm2008_prewar$c25time <-lcm2008_prewar$c26time <-lcm2008_prewar$c27time <-lcm2008_prewar$c28time <-lcm2008_prewar$c29time <-lcm2008_prewar$c30time <-lcm2008_prewar$c31time <-lcm2008_prewar$c32time <- lcm2008_prewar$c33time <-lcm2008_prewar$c34time <-lcm2008_prewar$c35time <-lcm2008_prewar$c36time <-lcm2008_prewar$c37time <-lcm2008_prewar$c38time <-lcm2008_prewar$c39time <-lcm2008_prewar$c40time <-lcm2008_prewar$c41time <-lcm2008_prewar$c42time <-lcm2008_prewar$c43time <-lcm2008_prewar$c44time <-lcm2008_prewar$c45time <-lcm2008_prewar$c46time <-lcm2008_prewar$c47time <-lcm2008_prewar$c48time <-lcm2008_prewar$c49time <-lcm2008_prewar$c50time <-lcm2008_prewar$c51time <-lcm2008_prewar$c1time <- lcm2008_prewar$lavgdisttrancosts <-lcm2008_prewar$lavgdist_ll <-lcm2008_prewar$lavgdisttrancosts_ll <-lcm2008_prewar$trancosts_ll <-lcm2008_prewar$dpolity <-lcm2008_prewar$dpolity40 <-lcm2008_prewar$dopen2 <-lcm2008_prewar$dopen240 <-lcm2008_prewar$dfranchise40 <-lcm2008_prewar$dfranchise30 <-lcm2008_prewar$dopenhyby40 <-lcm2008_prewar$llandlabor <-lcm2008_prewar$open2_llandlabor <-lcm2008_prewar$openhyby_llandlabor <- NULL

## Drop vars with no association to missing values
lcm2008_prewar$iv_trade <- lcm2008_prewar$iv_PPPtrd_1 <-lcm2008_prewar$iv_PPPtrd_yby <-lcm2008_prewar$merge2 <-lcm2008_prewar$merge3 <- lcm2008_prewar$fakeyear <- lcm2008_prewar$sample <- lcm2008_prewar$sampa <-lcm2008_prewar$samp <- NULL

## Drop vars with no variation
lcm2008_prewar$nofe <- NULL

which( colnames(lcm2008_prewar) =="_merge" )
lcm2008_prewar <- lcm2008_prewar[, -c(25)]

## Imputation
set.seed(02138)
lcm2008_prewar.out <- amelia(lcm2008_prewar, m = 20, cs = "country1", ts = "year", polytime = 3, lags = c("open2", "polity", "openhyby"), empri = 0.01*nrow(lcm2008_prewar))

write.amelia(obj= lcm2008_prewar.out, file.stem = "LCM2008 WP Imp Data Prewar", format = "dta", separate = FALSE)

## Diagnostics
dim(lcm2008_prewar)
plot(lcm2008_prewar.out, which.vars = 3:25)

## Interwar period
## Load original dataset
lcm2008_interwar <- read.dta("LCM2008 WP Rep Data Interwar.dta")
head(lcm2008_interwar)
dim(lcm2008_interwar)

## Drop ID vars
lcm2008_interwar$country1 <- lcm2008_interwar$scode <- lcm2008_interwar$cty1 <- NULL

## Remove extra rows with no info
dim(lcm2008_interwar)
lcm2008_interwar <- lcm2008_interwar[-c(1625, 1624, 1623),]

## Imputation
set.seed(02138)
lcm2008_interwar.out <- amelia(lcm2008_interwar, m = 20, cs = "country", ts = "year", polytime = 3, lags = c("openppp", "polity", "openppphyby"), empri = 0.01*nrow(lcm2008_interwar))

write.amelia(obj= lcm2008_interwar.out, file.stem = "LCM2008 WP Imp Data Interwar", format = "dta", separate = FALSE)

## Diagnostics
dim(lcm2008_interwar)
plot(lcm2008_interwar.out, which.vars = 3:13)

## Postwar period
## Load original dataset
lcm2008_postwar <- read.dta("LCM2008 WP Rep Data Postwar.dta")
head(lcm2008_postwar)
dim(lcm2008_postwar)

## Drop ID vars
lcm2008_postwar$wbcode <- lcm2008_postwar$wbname <- lcm2008_postwar$ifscode <- lcm2008_postwar$ccode <- NULL

## Drop highly collinear variables (not in analysis model)
lcm2008_postwar$openreg1 <-lcm2008_postwar$openreg2 <- lcm2008_postwar$openreg3 <-lcm2008_postwar$openreg4 <- lcm2008_postwar$openreg5 <-lcm2008_postwar$openreg6 <- lcm2008_postwar$openreg7 <-lcm2008_postwar$openreg8 <- lcm2008_postwar$openh1 <-lcm2008_postwar$openh2 <- lcm2008_postwar$openh3 <-lcm2008_postwar$openh4 <- lcm2008_postwar$openh5 <-lcm2008_postwar$openh6 <- lcm2008_postwar$openh7 <-lcm2008_postwar$openh8 <- NULL

## Remove year dummies
head(lcm2008_postwar)
dim(lcm2008_postwar)
lcm2008_postwar <- lcm2008_postwar[, -c(36:85)]

## Imputation
set.seed(02138)
lcm2008_postwar.out <- amelia(lcm2008_postwar, m = 20, cs = "wb", ts = "year", polytime = 3, lags = c("openppp", "openppphyby", "polity"), empri = 0.01*nrow(lcm2008_postwar))

write.amelia(obj=lcm2008_postwar.out, file.stem = "LCM2008 WP Imp Data Postwar", format = "dta", separate = FALSE)
